using System;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;
using OracleInternal.Network;

namespace OracleInternal.TTC
{
	internal class TTCVersion : TTCFunction
	{
		internal const int VSN_BANNER_FORMAT_BASE = 0;

		internal const int VSN_BANNER_FORMAT_FULL = 1;

		private byte[] m_rdbmsVersion = new byte[18]
		{
			78,
			111,
			116,
			32,
			100,
			101,
			116,
			101,
			114,
			109,
			105,
			110,
			101,
			100,
			32,
			121,
			101,
			116
		};

		private int m_bufferLen = 256;

		private int m_retVersionLength;

		internal long m_retVersionNumber;

		internal TTCVersion(MarshallingEngine marshallingEngine)
			: base(marshallingEngine, 59, 0)
		{
		}

		internal override void ReInit(MarshallingEngine marshallingEngine)
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCVersion, OracleTraceFuncName.ReInit);
			}
			try
			{
				base.ReInit(marshallingEngine);
				m_bufferLen = 256;
				m_retVersionLength = 0;
				m_retVersionNumber = 0L;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCVersion, OracleTraceFuncName.ReInit, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCVersion, OracleTraceFuncName.ReInit);
				}
			}
		}

		internal void ReadResponse()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCVersion, OracleTraceFuncName.ReadResponse);
			}
			bool flag = false;
			try
			{
				m_marshallingEngine.TTCErrorObject.Initialize();
				bool flag2 = false;
				while (!flag2)
				{
					try
					{
						switch ((byte)m_marshallingEngine.UnmarshalUB1())
						{
						case 23:
							ProcessServerSidePiggybackFunction(0);
							break;
						case 8:
							if (flag)
							{
								throw new Exception("TTC Error");
							}
							m_retVersionLength = m_marshallingEngine.UnmarshalUB2();
							m_rdbmsVersion = m_marshallingEngine.UnmarshalCHR(m_retVersionLength);
							if (m_rdbmsVersion == null)
							{
								throw new Exception("TTC Error");
							}
							m_retVersionNumber = m_marshallingEngine.UnmarshalUB4();
							flag = true;
							break;
						case 15:
							m_marshallingEngine.TTCErrorObject.ReadWarning();
							break;
						case 4:
							m_marshallingEngine.TTCErrorObject.ReadErrorMessage();
							flag2 = true;
							break;
						case 9:
							if (m_marshallingEngine.HasEOCSCapability)
							{
								m_marshallingEngine.m_endOfCallStatus = m_marshallingEngine.UnmarshalUB4();
							}
							if (m_marshallingEngine.HasFSAPCapability)
							{
								m_marshallingEngine.m_endToEndECIDSequenceNumber = m_marshallingEngine.UnmarshalUB2();
							}
							flag2 = true;
							break;
						default:
							throw new Exception("TTC Error");
						}
					}
					catch (NetworkException ex)
					{
						if (ex.ErrorCode != 3111)
						{
							throw;
						}
						m_marshallingEngine.m_oracleCommunication.Reset();
					}
					catch (Exception)
					{
						if (m_marshallingEngine.m_oraBufRdr != null)
						{
							m_marshallingEngine.m_oraBufRdr.ClearState();
						}
						m_marshallingEngine.m_oracleCommunication.Break();
						m_marshallingEngine.m_oracleCommunication.Reset();
					}
				}
			}
			catch (Exception ex3)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCVersion, OracleTraceFuncName.ReadResponse, ex3);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCVersion, OracleTraceFuncName.ReadResponse);
				}
			}
		}

		internal int GetVersionNumber()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCVersion, OracleTraceFuncName.GetVersionNumber);
			}
			long num = 0L;
			try
			{
				num += (HelperClass.URShift(m_retVersionNumber, 24) & 0xFF) * 10000;
				if (m_marshallingEngine.NegotiatedTTCVersion >= 11)
				{
					num += (HelperClass.URShift(m_retVersionNumber, 16) & 0xFF) * 1000;
					num += (HelperClass.URShift(m_retVersionNumber, 12) & 0xF) * 100;
					num += (HelperClass.URShift(m_retVersionNumber, 4) & 0xF) * 10;
				}
				else
				{
					num += (HelperClass.URShift(m_retVersionNumber, 20) & 0xF) * 1000;
					num += (HelperClass.URShift(m_retVersionNumber, 12) & 0xF) * 100;
					num += (HelperClass.URShift(m_retVersionNumber, 8) & 0xF) * 10;
				}
				num += HelperClass.URShift(m_retVersionNumber, 0) & 0xF;
				return (int)num;
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCVersion, OracleTraceFuncName.GetVersionNumber, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCVersion, OracleTraceFuncName.GetVersionNumber);
				}
			}
		}

		internal void WriteMessage()
		{
			if (ProviderConfig.m_bTraceLevelPrivate)
			{
				Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131328, OracleTraceClassName.TTCVersion, OracleTraceFuncName.WriteMessage);
			}
			try
			{
				WriteFunctionHeader();
				bool notnull = true;
				m_marshallingEngine.MarshalO2U(notnull);
				m_marshallingEngine.MarshalSWORD(m_bufferLen);
				m_marshallingEngine.MarshalO2U(notnull);
				m_marshallingEngine.MarshalO2U(notnull);
				if (m_marshallingEngine.NegotiatedTTCVersion >= 11)
				{
					m_marshallingEngine.MarshalUB4(1L);
				}
			}
			catch (Exception ex)
			{
				OracleException.HandleError(OracleTraceLevel.Private, (OracleTraceTag)268566528, OracleTraceClassName.TTCVersion, OracleTraceFuncName.WriteMessage, ex);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPrivate)
				{
					Trace.Write(OracleTraceLevel.Private, (OracleTraceTag)131584, OracleTraceClassName.TTCVersion, OracleTraceFuncName.WriteMessage);
				}
			}
		}
	}
}
